//
//  ChannelModel.m
//  
//
//  Created by admin on 17/2/28.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "ChannelModel.h"
#import <YYModel.h>
#import "NSDictionary+Data.h"
#import "NetworkTool.h"

@interface ChannelModel ()<YYModel>
 
 @end

@implementation ChannelModel

+ (void)getNewsCateWithSuccBlock:(void(^)(NSArray *modelArr))succBlock{
    
    
 
        NSDictionary *input_data = @{@"source":@"2"};
        NSDictionary *parameters = @{@"data":[input_data getData]};
        NetworkTool *networkTool = [NetworkTool sharedInstance];
        NSString *urlString = @"http://toutiao.api.inping.com/qqnews/channel";

    //1.获得NSUserDefaults文件
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

        [networkTool POSTWithUrlString:urlString parameters:parameters success:^(id responseObject) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:NULL];
            
            NSArray *dictArr = dict[@"data"];
            NSArray *modelArr = [NSArray yy_modelArrayWithClass:[ChannelModel class] json:dictArr];
            succBlock(modelArr);
            //2.存储到偏好设置
            [userDefaults setObject:dict forKey:@"cate"];
            
        } failure:^(NSError *error) {
            //3.读取文件
            NSDictionary *cateJson = [userDefaults objectForKey:@"cate"];
            // 获取tList对应的字典数组
            NSArray *dictArr = cateJson[@"data"];
            // 字典数组转模型数组
            NSArray *modelArr = [NSArray yy_modelArrayWithClass:[ChannelModel class] json:dictArr];
            succBlock(modelArr);

            NSLog(@"%@",error);
        }];
    
    
    

}

//+ (void)getVideoCateWithSuccBlock:(void(^)(NSArray *modelArr))succBlock{
//    
//    //1.获得NSUserDefaults文件
//    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//    //3.读取文件
//    NSDictionary *cateJson = [userDefaults objectForKey:@"video_cate"];
//    
//        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//
//        NSDictionary *input_data = @{};
//        NSDictionary *parameters = @{@"data":[input_data getData]};
//        NetworkTool *networkTool = [NetworkTool sharedInstance];
//        NSString *urlString = @"http://toutiao.api.inping.com/news/video_cate";
//        
//        [networkTool POSTWithUrlString:urlString parameters:parameters success:^(id responseObject) {
//            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:NULL];
//            
//            NSArray *dictArr = dict[@"data"];
//            NSArray *modelArr = [NSArray yy_modelArrayWithClass:[ChannelModel class] json:dictArr];
//            succBlock(modelArr);
//            //1.获得NSUserDefaults文件
//            //2.存储到偏好设置
//            [userDefaults setObject:dict forKey:@"video_cate"];
//
//            
//        } failure:^(NSError *error) {
//            // 获取tList对应的字典数组
//            NSArray *dictArr = cateJson[@"data"];
//            // 字典数组转模型数组
//            NSArray *modelArr = [NSArray yy_modelArrayWithClass:[ChannelModel class] json:dictArr];
//            succBlock(modelArr);
//
//            NSLog(@"%@",error);
//        }];
//
//}


// 当 JSON 转为 Model 完成后，该方法会被调用。
// 你可以在这里对数据进行校验，如果校验不通过，可以返回 NO，则该 Model 会被忽略。
// 你也可以在这里做一些自动转换不能完成的工作。
- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *channelBlacklist = [userDefaults objectForKey:@"channelBlacklist"];

    if (channelBlacklist) {
        
    }
    NetworkTool *networkTool = [NetworkTool sharedInstance];
    [networkTool GETWithUrlString:@"" parameters:@{} success:^(id responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:NULL];
        [userDefaults setObject:dict forKey:@"channelBlacklist"];

    } failure:^(NSError *error) {
    }];
    NSString *channel = dic[@"channel"];
    
    if ([channel isEqualToString:@"汽车"]) return NO;
    if ([channel isEqualToString:@"时尚"]) return NO;
    if ([channel isEqualToString:@"情感"]) return NO;
    if ([channel isEqualToString:@"iphone"]) return NO;

    return YES;
}

- (NSString *)description {
    return [NSString stringWithFormat:@"%@ %@",_channel,self.id];
}

@end
